package com.xiaotao.test01.websocket;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.xiaotao.test01.entity.Result;
import lombok.extern.slf4j.Slf4j;

import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;
import java.util.Arrays;
import java.util.Base64;

/**
 * @author xiaotao
 */
@Slf4j
public class CustomerDeCoder implements Decoder.Text<Result> {

    @Override
    public Result decode(String s) throws DecodeException {
        byte[] decode = Base64.getDecoder().decode(s.getBytes());
        return JSONObject.parseObject(new String(decode), Result.class);
    }

    @Override
    public boolean willDecode(String s) {
        return s != null && !s.isEmpty();
    }

    @Override
    public void init(EndpointConfig endpointConfig) {

    }

    @Override
    public void destroy() {

    }
}
